const request = require('request')
var config = require('../appConfig').qqconfig;
var getQqaccesstoken = require('./getQqaccesstoken');
var getQueryString = require('./transqqBackData2obj');

/*
* https://graph.qq.com/oauth2.0/me?access_token=YOUR_ACCESS_TOKEN
*/

var getAccessToken_qq = function(code) {
    return new Promise(function (resolve, reject) {
        getQqaccesstoken(code).then(function (response) {
            var data = getQueryString(response);
            var url = config.openidUrl+'?access_token='+data.access_token
            request.get(url, (err, res, body) => {
                if (err) {
                    reject(err)
                }
                if (body.errcode) {
                    reject(body.errcode + body.errmsg)
                }
                var tempObj = {};
                tempObj['func'] = body;  // boby => callback( {"client_id":"YOUR_APPID","openid":"YOUR_OPENID"} );
                tempObj['access_token'] = data.access_token;
                resolve(tempObj)
            })
        })
    })

}
// 只要向外暴露一个获取值的方法就可以了
module.exports = getAccessToken_qq
